package com.github.shiqiyue.app.api.sapi.controller.sys;


import com.github.shiqiyue.app.api.sapi.common.SApiBaseController;
import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.common.utils.JsonUtils;
import com.github.shiqiyue.app.modules.sys.entity.RolePermission;
import com.github.shiqiyue.app.modules.sys.service.IRolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 * 角色-权限控制器
 * @author wenyao.wu
 * @date 2018/10/16
 */
@Slf4j
@Api(tags = "角色-权限")
@RestController
@RequestMapping("sapi/sys/role-permission")
public class SApiRolePermissionController extends SApiBaseController {


    @Autowired
    private IRolePermissionService rolePermissionService;

    /***
     * 获取角色对应的所有权限
     * @param roleId
     * @return
     */
    @PreAuthorize("hasAuthority('0030104')")
    @ApiOperation("获取角色对应的所有权限")
    @PostMapping("/search/role-id/{roleId}")
    public Result<List<RolePermission>> findByRoleId(@PathVariable("roleId") Long roleId) {
        List<RolePermission> rolePermissions = rolePermissionService.findByRoleId(roleId);
        return Result.success(rolePermissions);
    }

    /***
     * 保存角色的权限
     * @param roleId
     * @param rolePermissionListJson
     * @return
     */
    @PreAuthorize("hasAuthority('0030104')")
    @ApiOperation("保存角色的权限")
    @PostMapping("save")
    public Result save(Long roleId, String rolePermissionListJson) {
        List<RolePermission> rolePermissions = JsonUtils.stringToBeanList(rolePermissionListJson, RolePermission.class);
        rolePermissionService.updateByRoleId(roleId, rolePermissions);
        return Result.success();
    }
}
